using System;
using System.Xml;
using System.Data;
using System.Web;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Security.Cryptography;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
using AliCMS.SystemModule;

namespace AliCMS
{
	public abstract class ObjectBase
    {
        public ObjectBase(int instanceID, string lang, bool checkRight)
		{
            _instanceID = instanceID;
			_lang = lang;
            _checkRight = checkRight;
		}

		public virtual void Dispose()
		{
		}
        public virtual object ExecuteService(HttpContext context)
        {
            return null;
        }
        public virtual void AliUpload(string fieldName, ref string uploadFile, bool isLast)
        {
        }

        protected virtual DataTable _List(int page, int pageSize, Dictionary<string, object> condition, Dictionary<string, string> sort)
        {
            return null;
        }
        protected DataTable HandleCommonList(HttpContext context)
        {
            string value = null;

            int page = -1;
            value = context.Request.QueryString["Page"];
            if (value != null) page = Convert.ToInt32(value);

            int pageSize = -1;
            value = context.Request.QueryString["PageSize"];
            if (value != null) pageSize = Convert.ToInt32(value);

            Dictionary<string, object> condition = null;
            value = context.Request.QueryString["Condition"];
            if (value != null)
            {
                condition = new Dictionary<string, object>();
                string[] conditions = value.Split(';');
                foreach (string pair in conditions)
                {
                    if (String.IsNullOrEmpty(pair))
                        continue;

                    int index = pair.IndexOf(':');
                    condition[pair.Substring(0, index)] = pair.Substring(index + 1);
                }
            }

            Dictionary<string, string> sort = null;
            value = context.Request.QueryString["Sort"];
            if (value != null)
            {
                sort = new Dictionary<string, string>();
                string[] sorts = value.Split(';');
                foreach (string pair in sorts)
                {
                    int index = pair.IndexOf(':');
                    sort[pair.Substring(0, index)] = pair.Substring(index + 1);
                }
            }

            return _List(page, pageSize, condition, sort);
        }

        protected void ValidateRight(string operationName, Guid recordID)
        {
            string objectName = GetType().ToString().Substring(GetType().ToString().LastIndexOf(".") + 1).Replace('+', '.');
            if (!Manager.HasRight(null, _instanceID, _lang, objectName, operationName, recordID))
                throw new Exception("lang_no_right");
        }

        #region Properties
        public IManager Manager
        {
            get
            {
                if (_manager == null)
                    return (IManager)HttpContext.Current.Application["Manager"];

                return _manager;
            }
            set
            {
                _manager = value;
            }
        }
        protected int InstanceID
		{
			get { return _instanceID; }
		}
        protected string Lang
		{
			get { return _lang; }
		}
        protected bool CheckRight
		{
            get { return _checkRight; }
            set { _checkRight = value; }
		}
        #endregion

		#region Fields
        private IManager _manager = null;
        private int _instanceID;
        private string _lang;
        private bool _checkRight;
        //private DataTable _generate;
        #endregion
	}
}
